/**
 * Created by hcadmin on 16/4/18.
 */
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function ($, IO, upui, up, jui, bootbox, definebt) {
    $(function () {
        var realname= $("#realname").val();
        gettable(realname);
        $("#select-btn").bind("click", function () {
            var realname= $("#realname").val();
            gettable(realname);
        });


        //获得省市级医生总数
        var pamars={};
        pamars.doctor_type_id=2;//医生类型1=普通医生，2=省市级，3=名医
        pamars.page=1;
        pamars.rows=10;
        pamars.realname="";
        IO.post("/api/doctorlist",pamars,function(d){
            if(d.code=="0"){
                var  count= d.data.total;
                $("#count").text(count);
            }
        })
    });

    function gettable(realname){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '医生ID',
                dataIndex: ' doctor_id',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" + data[datarow]. doctor_id + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '医生姓名',//-----------------------
                dataIndex: 'realname',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].realname + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '类别',
                dataIndex: 'doctor_type_name',
                dataSource: function (data, datarow, gridobj, current_column) {

                    if(data[datarow]['doctorType']==null){
                        return "<span>undefined</span>";

                    }else{
                        return "<span>" + data[datarow]['doctorType'].doctor_type_name + "</span>";
                    }


                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '医院',
                dataIndex: 'hospital_name',//-----------------------------------
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow]['hospital']==null){
                        return "<span>undefined</span>";

                    }else{
                        return "<span>" + data[datarow]['hospital'].hospital_name + "</span>";
                    }

                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '科室',
                dataIndex: 'department_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow]['department']==null){
                        return "<span>undefined</span>";

                    }else{
                        return "<span>" + data[datarow]['department'].department_name + "</span>";
                    }

                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '职称',
                dataIndex: 'duty_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow]['duty']==null){
                        return "<span>undefined</span>";

                    }else{
                        return "<span>" + data[datarow]['duty'].duty_name+ "</span>";
                    }


                }
            },
            {
                title: '职务评分',
                dataIndex: 'service_avgscore',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].service_avgscore + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: ' 粉丝数',
                dataIndex: 'fans_count',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].fans_count + "</span>";

                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '服务金额(元/单)',
                dataIndex: 'asset',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].consult_fee + "</span>";

                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '服务单数',
                dataIndex: 'service_count',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].service_count + "</span>";

                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '账户金额',//---------------------------------------
                dataIndex: 'asset',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].asset + "</span>";

                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '查看详情',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='#' class='editmodal' data-toggle='modal' data-target='#myModal' act_id="+data[datarow].doctor_id+"><span style='color: #6ab7ad;'>" + "查看详情" + "</span></a>";

                }
            }
        ];
        //数组初始化
        function dataInit(realname) {

            grid = new Grid("/api/doctorlist", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(realname), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param(realname) {

            var param="realname="+realname+"&doctor_type_id=2";
            return param;
        }
        dataInit(realname);
    }
    var doctor_id;
    //详情按钮
    $(document).on("click",".editmodal",function(){

        var act_id=$(this).attr("act_id");
        doctor_id=act_id;
        if(doctor_id){
            //获得申请详情
            var pamars={};
            pamars.doctor_id=doctor_id;
            IO.post("/api/doctorDetails",pamars,function(d){
                if(d.code=="0"){
                    $("#realname2").val(d.data.realname);//真实姓名
                    doctor_type_id_Show(d.data.doctorType.doctor_type_id);//医生等级
                    $("#service_count").val(d.data.service_count);//服务单数
                    $("#fans_count").val(d.data.fans_count);//粉丝数
                    $("#idcard").val(d.data.idcard);//身份证号
                    $("#card_num").val(d.data.card_num);//银行卡号
                    $("#accountBalance").val(d.data.asset);//当前余额
                    province_city_area_nameShow(d.data.province_id,d.data.city_id,d.data.area_id);//省,市,区
                    hospital_department_jobTitle_nameShow(d.data.hospital.hospital_id,d.data.department.department_id,d.data.duty.duty_id);//医院全称,科室,职称
                    $("#departmentTel").val(d.data.depart_phone);//科室电话
                    $(".addimage1").attr( "src",d.data.pic_neg); //手持证件照
                    $(".addimage2").attr( "src",d.data.pic_pos );//正面免冠照
                    $("#goodCase").val(d.data.specialty);//擅长病症
                    $("#txtShow").val(d.data.introduction);//个人简介
                    $("#newcard_ability").val(d.data.newcard_ability);//建卡能力
                }
            })

            //先查数据绑定到页面上,然后手动调用modal
            $('#myModaledit').modal();
        }
    });

    //点击编辑按钮
    $(document).on("click","#btn_update",function(){
        //获得点击状态
        var index=$("#btn_some").val();
        if(index==0){//进入编辑状态
            $("#btn_some").val(1);
            $("#btn_update").text("保存");
            $(".ylm1").removeAttr("readOnly");
            $(".ylm1").removeAttr("disabled",false);
            $(".ylm2").css("background","gray");
            $('.form-control').attr("disabled",false);
            $(".form-control").removeAttr("readOnly");
        }
        else{//保存
            //获得用户数据
            var pamars={};
            pamars.realname=$("#realname2").val();//真实姓名
            pamars.doctor_type_id=$("#doctor_type_id").val();//医生等级
            pamars.service_count=$("#service_count").val();// 服务单数
            pamars.fans_count=$("#fans_count").val();// 粉丝数
            pamars.idcard=$("#idcard").val();//身份证号
            pamars.card_num=$("#card_num").val();//银行卡号
            pamars.accountBalance=$("#accountBalance").val();//当前账户余额
            pamars.province_id=$("#province_id").val();//省
            pamars.city_id=$("#city_id").val();//市
            pamars.area_id=$("#area_id").val();//区
            pamars.hospital_id=$("#hospital_id").val();//医院全称
            pamars.department_id=$("#department_id").val();//科
            pamars.depart_phone=$("#departmentTel").val();//科室电话
            pamars.duty_id=$("#jobTitle_id").val();//职称
            pamars.pic_neg=$("#upimgs1").attr("src");//手持证件照
            pamars.pic_pos=$("#upimgs2").attr("src");//正面免冠照
            pamars.specialty=$("#goodCase").val();//擅长
            pamars.introduction=$("#txtShow").val();//个人介绍
            pamars.doctor_id=doctor_id;//医生ID
            pamars.newcard_ability=$("#newcard_ability").val();//建卡能力
            if( pamars.doctor_type_id=='-1'){
               alert('请选择医生等级');
                return;
            }

            if(pamars.realname==""){
               alert('真实姓名不能为空');
                return;
            }
            if(pamars.password==""){
                alert('初始密码不能为空');
                return;
            }
            if(pamars.idcard==""){
               alert('身份证号不能为空');
                return;
            }
            if(pamars.card_num==""){
               alert('银行卡号不能为空');
                return;
            }
            if(pamars.province_id=="-1"){
                alert('省不能为空');
                return;
            }
            if(pamars.city_id=="-1"){
                alert('市不能为空');
                return;
            }
            if(pamars.area_id=="-1"){
               alert('区不能为空');
                return;
            }
            if(pamars.hospital_id=="-1"){
                alert('医院全称不能为空');
                return;
            }
            if(pamars.department_id=="-1"){
               alert('科不能为空');
                return;
            }
            if(pamars.depart_phone==""){
                alert('科室电话不能为空');
                return;
            }
            if(pamars.duty_id=="-1"){
                alert('职称不能为空');
                return;
            }
            if(pamars.newcard_ability=="-2"){
                alert('建卡能力不能为空');
                return;
            }
            if(pamars.specialty==""){
                alert('擅长不能为空');
                return;
            }
            if(pamars.specialty.length>50){
                alert('擅长病例字数不能超过50');
                return;
            }
            if(pamars.pic_neg=="/images/ylm.png"){
                alert('请上传手持证件照');
                return;
            }
            if(pamars.pic_pos=="/images/ylm.png"){
                alert('请上传正面免冠照');
                return;
            }
            if(pamars.introduction==""){
                alert('请填写自我介绍');
                return;
            }
            if(pamars.introduction.length>400){
                alert('个人介绍字数不能超过400');
                return;
            }
            IO.post("/api/doctorUpdate",pamars,function(d){
                if(d.code=="0"){
                    $("#btn_some").val(0);
                    $("#btn_update").text("编辑医生信息");
                    $('#myModal').modal("hide");
                    bootbox.alert({
                        buttons: {
                            ok: {
                                label: '确定',
                                className: 'btn-myStyle'
                            }
                        },
                        message:  d.msg,
                        callback: function() {
                            location.href = '/96053/provincialcity';
                        },
                        title: "提示",
                    });



                }else{
                    $('#myModal').modal("hide");
                    bootbox.alert({
                        buttons: {
                            ok: {
                                label: '确定',
                                className: 'btn-myStyle'
                            }
                        },
                        message:  d.msg,
                        callback: function() {
                            //location.href = '/96053/provincialcity';
                        },
                        title: "提示",
                    });
                }
            });



        }
    })


    //$('#myModal').on('hidden.bs.modal', function () {
    //
    //    location.href = '/96053/provincialcity';
    //
    //});

    //医院列表的省市区的三级联动
    $("#province_id").on("change",function(){

        var provinceList = JSON.parse($("#provinceA").val());
        var obj;
        $("#city_id").empty();
        $("#city_id").append("<option value='-1' selected>请选择</option>");
        $("#area_id").empty();
        $("#area_id").append("<option value='-1' selected>请选择</option>");
        for (obj in provinceList){
            if(provinceList[obj].name == $("#province_id").find("option:selected").text()){
                for (var x in provinceList[obj].cityList){
                    var city = provinceList[obj].cityList;
                    $("#city_id").append("<option value='"+city[x].code+"'>"+city[x].name+"</option>");
                }
            }
        }
        hospitalShow();
    })
    $("#city_id").on("change",function(){

        var provinceList = JSON.parse($("#provinceA").val());
        var obj;
        $("#area_id").empty();
        $("#area_id").append("<option value='-1' selected>请选择</option>");
        for (obj in provinceList){
            if(provinceList[obj].name == $("#province_id").find("option:selected").text()){
                var city = provinceList[obj].cityList;
                for (var x in city){
                    if ($("#city_id").find("option:selected").text()==city[x].name){
                        var area = city[x].areaList;
                        for (var y in area){
                            $("#area_id").append("<option value='"+area[y].code+"'>"+area[y].name+"</option>");
                        }

                    }

                }
            }
        }
        hospitalShow();
    })
    $("#area_id").on("change",function(){
        hospitalShow();
    });
    function hospitalShow(){
        var pamars={};
        pamars.province_id=$('#province_id').val()=="-1"? "":$('#province_id').val();
        pamars.city_id=$('#city_id').val()=="-1"? "":$('#city_id').val();
        pamars.area_id=$('#area_id').val()=="-1"? "":$('#area_id').val();
        IO.post("/96053/api/hostpital",pamars,function(d){
            if(d.code=="0"){
                $("#hospital_id").empty();
                $("#hospital_id").append("<option value='-1' selected>医院全称</option>");
                for (var i=0;i< d.data.rows.length;i++){

                    $("#hospital_id").append("<option value='"+d.data.rows[i].hospital_id+"'>"+d.data.rows[i].hospital_name+"</option>");
                }
            }
        });
    }
    //选取医院科室名称改变联动职称名称联动
    $("#hospital_id").on("change",function(){
        $("#department_id").empty();
        $("#jobTitle_id").empty();
        //获得所属医院的科室名称
        var pamars={};
        pamars.hospital_id= $("#hospital_id option:selected").val();
        $("#department_id").append("<option value='-1' selected>请选择</option>");
        $("#jobTitle_id").append("<option value='-1' selected>请选择</option>");
        IO.post("/api/hospitalsAllDepartments",pamars,function(d){
            if(d.code=="0"){
                for(var i=0;i< d.data.length;i++){
                    $("#department_id").append("<option value='"+d.data[i].department_id+"'>"+d.data[i].department_name+"</option>");
                }
            }
        });
        IO.post("/api/hospitalsAllDuty",pamars,function(d){
            if(d.code=="0"){
                for(var i=0;i< d.data.length;i++){
                    $("#jobTitle_id").append("<option value='"+d.data[i].duty_id+"'>"+d.data[i].duty_name+"</option>");
                }
            }
        });
    });

    //根据省id,城市id,区id,初始数据到省,市,区,三个下拉框
    function province_city_area_nameShow(province_id,city_id,area_id){
        var provinceList = JSON.parse($("#provinceA").val());

        for ( var obj in provinceList) {
            if (provinceList[obj].code == province_id) {
                $("#province_id").val(provinceList[obj].code);//下拉框绑定省
                //获得省所有市,并读出当前用户所在市
                $("#city_id").empty();
                $("#city_id").append("<option value='-1' selected>请选择</option>");
                for (var obj1 in provinceList){
                    if(provinceList[obj1].name == $("#province_id").find("option:selected").text()){
                        for (var x in provinceList[obj1].cityList){
                            var city = provinceList[obj1].cityList;
                            $("#city_id").append("<option value='"+city[x].code+"'>"+city[x].name+"</option>");
                        }
                        $("#city_id").val(city_id);//下拉框绑定市
                        $("#area_id").empty();
                        $("#area_id").append("<option value='-1' selected>请选择</option>");
                        for (var obj2 in provinceList){
                            if(provinceList[obj2].name == $("#province_id").find("option:selected").text()){
                                var city2 = provinceList[obj2].cityList;
                                for (var x2 in city2){
                                    if ($("#city_id").find("option:selected").text()==city[x2].name){
                                        var area = city[x2].areaList;
                                        for (var y in area){
                                            $("#area_id").append("<option value='"+area[y].code+"'>"+area[y].name+"</option>");
                                        }
                                        $("#area_id").val(area_id);//下拉框绑定市
                                    }

                                }
                            }
                        }

                    }
                }

            }
        }
    }
    //根据医院id初始化医院下拉框数据
    function hospital_department_jobTitle_nameShow(hospital_id,department_id,jobTitle_id){
        var pamars={};
        pamars.province_id=$('#province_id').val();
        pamars.city_id=$('#city_id').val();
        pamars.area_id=$('#area_id').val();
        IO.post("/96053/api/hostpital",pamars,function(d){
            if(d.code=="0"){
                $("#hospital_id").empty();
                $("#hospital_id").append("<option value='-1' selected>医院全称</option>");
                for (var i=0;i< d.data.rows.length;i++){
                    $("#hospital_id").append("<option value='"+d.data.rows[i].hospital_id+"'>"+d.data.rows[i].hospital_name+"</option>");
                }
                $("#hospital_id").val(hospital_id);//下拉框绑定医院全称

                var pamar={};
                pamar.hospital_id= $("#hospital_id option:selected").val();
                $("#department_id").empty();
                $("#jobTitle_id").empty();
                $("#department_id").append("<option value='-1' selected>请选择</option>");
                $("#jobTitle_id").append("<option value='-1' selected>请选择</option>");

                IO.post("/api/hospitalsAllDepartments",pamar,function(d){
                    if(d.code=="0"){
                        for(var i=0;i< d.data.length;i++){
                            $("#department_id").append("<option value='"+d.data[i].department_id+"'>"+d.data[i].department_name+"</option>");
                        }
                        $("#department_id").val(department_id);//下拉框绑定科室
                    }
                });
                IO.post("/api/hospitalsAllDuty",pamar,function(d){
                    if(d.code=="0"){
                        for(var i=0;i< d.data.length;i++){
                            $("#jobTitle_id").append("<option value='"+d.data[i].duty_id+"'>"+d.data[i].duty_name+"</option>");
                        }
                        $("#jobTitle_id").val(jobTitle_id);//下拉框绑定职称
                    }
                });
            }
        })
    }
    //根据医生类别id 绑定下拉框
    function doctor_type_id_Show(doctor_type_id){
        //获得医院所有医生类型
        var pamars2={};
        IO.post("/api/doctorTypeAll",pamars2,function(d){
            if(d.code=="0"){
                $("#doctor_type_id").empty();
                $("#doctor_type_id").append("<option value='-1' selected>请选择</option>");
                for (var i=0;i< d.data.length;i++){
                    $("#doctor_type_id").append("<option value='"+d.data[i].doctor_type_id+"'>"+d.data[i].doctor_type_name+"</option>");
                }
                $("#doctor_type_id").val(doctor_type_id);//下拉框绑定医院全称
            }
        });
    }

    //手持证件照片上传
    $('#fileupload1').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        document.getElementById('upimgs1').src = '/images/loading.gif';
    }).on('fileuploadprocessalways', function (e, data) {

    }).on('fileuploadprogressall', function (e, data) {

    }).on('fileuploaddone', function (e, data) {
        var alt = $(e.target).attr('alt');
        if (!alt)
            alt = '';

        $("#upimgs1" + alt).attr("src", data._response.result.url);
    });

    //正面证件照片上传
    $('#fileupload2').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        document.getElementById('upimgs2').src = '/images/loading.gif';
    }).on('fileuploadprocessalways', function (e, data) {

    }).on('fileuploadprogressall', function (e, data) {

    }).on('fileuploaddone', function (e, data) {
        var alt = $(e.target).attr('alt');
        if (!alt)
            alt = '';

        $("#upimgs2" + alt).attr("src", data._response.result.url);
    });


});
/**
 * Created by hcadmin on 16/4/18.
 */
$('#myModal').on('hide.bs.modal', function () {
    $("#btn_some").val(0);
    $("#btn_update").text("编辑普通信息");
    $(".ylm1").attr("readOnly","true");
    $(".ylm1").attr("disabled","disabled");
    $(".ylm2").css("background",'');
    $('.form-control').attr("disabled","disabled");
    $(".form-control").attr("readOnly","true");
})